﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeneralClasses;

namespace Interface
{
    public partial class frmPayGrades : Form
    {
        Form opener;

        List<PayGrade> payGrades;
        PayGrade selectedPayGrade;

        public frmPayGrades(Form Opener)
        {
            opener = Opener;
            InitializeComponent();
        }

        void refresh()
        {
            selectedPayGrade = null;

            payGrades = PayGrade.getAllPayGrades();

            txtEditPayGradeName.Text = "";
            nudEditPayGradePay.Value = 0;

            txtAddPayGradeName.Text = "";
            nudPayGradePay.Value = 0;

            dgvPayGrades.Rows.Clear();
            foreach (PayGrade item in payGrades)
            {
                dgvPayGrades.Rows.Add(item.Name, Convert.ToString(item.Wage));
            }
        }

        private void btnDeleteDepartment_Click(object sender, EventArgs e)
        {
            PayGrade payGrade = new PayGrade();

            if (dgvPayGrades.SelectedRows.Count > 0)
            {
                foreach (PayGrade item in payGrades)
                {
                    if (item.Name == dgvPayGrades.SelectedRows[0].Cells[0].Value.ToString())
                    {
                        payGrade = item;
                        break;
                    }
                }

                if (payGrade != null)
                {
                    if (payGrade.removeData() == 0)
                    {
                        refresh();
                    }
                    else
                    {
                        MessageBox.Show("Problem deleting paygrade!");
                    }
                }
            }
        }

        private void btnAddPayGrade_Click(object sender, EventArgs e)
        {
            if (txtAddPayGradeName.Text == "")
            {
                MessageBox.Show("Please enter a name for the pay grade.");
                txtAddPayGradeName.Focus();
            }
            else
            {
                selectedPayGrade = new PayGrade();
                selectedPayGrade.Name = txtAddPayGradeName.Text;
                selectedPayGrade.Wage = (float)nudPayGradePay.Value;

                if (selectedPayGrade.saveData() < 0)
                {
                    MessageBox.Show("Problem saving paygrade!");
                }
                else
                {
                    refresh();
                }
            }
        }

        private void btnEditPayGrade_Click(object sender, EventArgs e)
        {
            if (selectedPayGrade != null)
            {
                selectedPayGrade.Name = txtEditPayGradeName.Text;
                selectedPayGrade.Wage = (float)nudEditPayGradePay.Value;

                if (selectedPayGrade.saveData() < 0)
                {
                    MessageBox.Show("Problem editing paygrade!");
                }
                else
                {
                    refresh();
                }
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmPayGrades_FormClosing(object sender, FormClosingEventArgs e)
        {
            opener.Show();
        }

        private void frmPayGrades_Shown(object sender, EventArgs e)
        {
            refresh();
        }

        private void dgvPayGrades_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvPayGrades.SelectedRows.Count > 0)
            {
                foreach (PayGrade item in payGrades)
                {
                    if (item.Name == dgvPayGrades.SelectedRows[0].Cells[0].Value)
                    {
                        selectedPayGrade = item;
                        break;
                    }
                }
            }

            if (selectedPayGrade != null)
            {
                txtEditPayGradeName.Text = selectedPayGrade.Name;
                nudEditPayGradePay.Value = (decimal)selectedPayGrade.Wage;
            }
        }

        private void txtAddPayGradeName_TextChanged(object sender, EventArgs e)
        {
            Input_Constraint.keepOnly(txtAddPayGradeName, true, true, true, false, null);
        }

        private void txtEditPayGradeName_TextChanged(object sender, EventArgs e)
        {
            Input_Constraint.keepOnly(txtEditPayGradeName, true, true, true, false, null);
        }


    }
}
